package ibm.cognos.imt.chart

import ibm.cognos.imt.Filter
import ibm.cognos.imt.ReportElement
import ibm.cognos.imt.dataUsage.Categories
import ibm.cognos.imt.dataUsage.Measure
import ibm.cognos.imt.dataUsage.Slices

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 1/27/14
 * Time: 9:56 AM
 * To change this template use File | Settings | File Templates.
 */
class CombinationChart extends ReportElement{

    def dataItems
    def queryName
    def name
    def categories
    def measure
    def slices
    def owner

    def specification

    CombinationChart(report, items) {
        owner = report
        dataItems = items
        items.each {
            if (it instanceof Slices) {
                this.slices = it.getDataItem().name
            }
            if (it instanceof Categories) {
                this.categories = it.getDataItem().name
            }
            if (it instanceof Measure) {
                this.measure = it.getDataItem().name
                it.getDataItem().aggregate = "total"
            }
        }
    }

    CombinationChart(){

    }

    CombinationChart(node) {
        name = node.@name
        queryName = node.@refQuery
        slices = node.combinationChartTypes.line.chartNodes.chartNode.chartNodeMembers.chartNodeMember.@refDataItem
        measure = node.defaultChartMeasure.@refDataItem
        categories = node.commonClusters.chartNodes.chartNode.chartNodeMembers.chartNodeMember.@refDataItem
    }

    def toPieChart() {
        PieChart chart = new PieChart()
        chart.owner = owner
        chart.name = name
        chart.measure = measure
        chart.queryName = queryName
        chart.slices = slices
        chart.categories = categories
        chart
    }


    def filter(exp) {
        def filter = new Filter(exp, owner)
//        update query list of the report
//        ListIterator li = owner.queryList.listIterator()
//        while (li.hasNext()) {
//            def query = li.next()
//            if (query.name == queryName) {
//                li.set(query.addFilter(filter))
//            }
//        }
        owner.queryMap[(queryName)].addFilter(filter)
        this
    }

    def generateSpec(builder) {
        builder.combinationChart(showTooltips: "true", maxHotspots: "100000", refQuery: "$queryName", name: "$name") {
            legend {
                legendPosition {
                    relativePosition()
                }
                legendTitle(refQuery: "$queryName") {
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "lx")
                        }
                    }
                }
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "lg")
                    }
                }
            }
            ordinalAxis {
                axisTitle(refQuery: "$queryName") {
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "at")
                        }
                    }
                }
                axisLine(color: "black")
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "al")
                    }
                }
            }
            numericalAxisY1 {
                axisTitle(refQuery: "$queryName") {
                    style {
                        defaultStyles {
                            defaultStyle(refStyle: "at")
                        }
                    }
                }
                gridlines(color: "#cccccc")
                axisLine(color: "black")
                style {
                    defaultStyles {
                        defaultStyle(refStyle: "al")
                    }
                }
            }
            combinationChartTypes {
                if (slices != null) {
                    bar {
                        chartNodes {

                            chartNode {
                                chartNodeMembers {
                                    chartNodeMember(refDataItem: "$slices") {
                                        chartContents {
                                            chartTextItem {
                                                dataSource {
                                                    memberCaption()
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {

                    bar()
                }
            }
            style {
                defaultStyles {
                    defaultStyle(refStyle: "ch")
                }
            }
            commonClusters {
                chartNodes {
                    chartNode {
                        chartNodeMembers {
                            chartNodeMember(refDataItem: "$categories") {
                                chartContents {
                                    chartTextItem {
                                        dataSource {
                                            memberCaption()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            defaultChartMeasure(refDataItem: "$measure")
        }

    }
}
